﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.System.Controllers.Dist
{
    public class AuditSetController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "System_AuditSet"; }
        }

        public ActionResult search()
        {
            InitSelectList();
            return View(GetList());
        }

        public ActionResult Index()
        {
            InitSelectList();
            return View(GetList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            InitSelectList();

            return View(GetList());
        }

       protected List<GSLMS.Entity.TB_SYS_AUDITSET> GetList()
        {
             var searchParma=new TB_SYS_AUDITSET();

            if (ViewData["SearchParma"] != null)
            {
                 searchParma = ViewData["SearchParma"] as TB_SYS_AUDITSET;
               
            }

          return  serviceSysAuditSet.GetList(searchParma);
        }

       

        public ActionResult Create(long id)
        {

            GSLMS.Entity.TB_SYS_AUDITSET model = new GSLMS.Entity.TB_SYS_AUDITSET();
            
           
            InitSelectList();

            model = serviceSysAuditSet.GetByID(id);
            if (model == null)
            {
                model = new TB_SYS_AUDITSET();

            }
            model.CREATEBY = CurentUser.UserName;
            model.CREATEDATE = DateTime.Now;

            return View(model);
        }

        //
        // POST: /Dictionary/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                GSLMS.Entity.TB_SYS_AUDITSET model = new GSLMS.Entity.TB_SYS_AUDITSET();
                TryUpdateModel(model, collection);
                bool result =false;
                result = model.ID > 0 ? serviceSysAuditSet.Update(model) : serviceSysAuditSet.Add(model);
                if (result)
                {

                    ajaxStr = string.Format(ajaxStr, 200,
                        ResourceManager.SaveSuccess,
                        navTabId,
                        "",
                         "closeCurrent");
                }
                else 
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /Dictionary/Delete/5
        [HttpPost]
        public string Delete(long id)
        {
            try
            {
                if (serviceSysAuditSet.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            InitSelectList();

            return "{" + ajaxStr + "}";
            
        }

        private void InitSelectList()
        {


            var searchParma = new GSLMS.Entity.TB_SYS_AUDITSET();
           if (ViewData["SearchParma"] != null)
           {
               searchParma = ViewData["SearchParma"] as TB_SYS_AUDITSET;
           }


           ViewData["SearchParma"] = searchParma ?? new TB_SYS_AUDITSET();

            ViewData["SpecailTypeList"] = EnumHelper.ToSelectList<SystemEnum.SpecailType>();
            ViewData["UserDataList"] = new SelectList(serviceEmp.GetUserEffList(), "username", "empname");
            

        }


    }
}
